import { defineStore } from "pinia";
import {
    getUserInfo,
    getToken,
    clearAuthStorage,
    setLocalToken,
    setLocalUserInfo,
} from "./helpers";
import { ref } from "vue";
import { UserInfo } from "@tarojs/taro";

export const useAuthStore = defineStore("auth-store", () => {
    const user = ref(getUserInfo());
    const token = getToken();
    function isLogin() {
      return Boolean(token)
    }
    function resetAuthStore() {
        clearAuthStorage();
        this.$reset();
    }

    function setToken(token: string) {
        this.token = token;
        setLocalToken(token);
    }
    function setUserInfo(user: Auth.UserInfo) {
        this.user = user;
        setLocalUserInfo(user);
    }
    return {
        user,
        token,
        isLogin,
        resetAuthStore,
        setToken,
        setUserInfo
    };
});

// export const useAuthStore = defineStore('auth-store', {
//   state: (): ({
//     user?: getUserInfo(),
//     token: getToken()
//   }),
//   getters: {
//     /** 是否登录 */
//     isLogin: state => Boolean(state.token)
//   },
//   actions: {
//     /** 重置auth状态 */
//     resetAuthStore() {
//       clearAuthStorage();
//       this.$reset();
//     }
//   }
// });
